import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_blog_manage_study/res/r.dart';
import 'package:flutter_blog_manage_study/res/strings.dart';
import 'package:flutter_blog_manage_study/res/style.dart';
import 'package:flutter_blog_manage_study/widget/over_scroll_behavior.dart';
import 'package:get/get.dart';
import 'package:lottie/lottie.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../base/get/controller/base_page_controller.dart';

/// 刷新状态
enum Refresh {
  /// 初次进入页面加载
  first,

  /// 上拉加载
  pull,

  /// 下拉加载
  down,
}

/// * 作者: Tao
/// * 日期: 2024/3/20
/// * 时间: 10:26
/// * 描述: 刷新组件，上拉加载、下拉刷新、空白页加载
/// 通过在BasePageController中维护上拉加载、下拉刷新、空白页加载的状态，来实现刷新组件的使用
/// 此只对Getx框架有效
class PullSmartRefreshWidget<Controller extends BaseGetPageController>
    extends StatefulWidget {
  PullSmartRefreshWidget({
    super.key,
    this.enablePullUp = true,
    this.enablePullDown = true,
    this.onRefresh,
    this.onLoadMore,
    required this.child,
  });

  final String? tag = null;

  /// 获取BaseGetController子类对象，在GetX中，任何BaseGetController都可以通过此方法获取
  /// 但是必须是没有dispose的Controller
  Controller get getController => GetInstance().find<Controller>(tag: tag);

  /// 是否启用上拉
  bool enablePullUp = true;

  /// 是否启用下拉
  bool enablePullDown = true;

  /// 下拉刷新回调
  VoidCallback? onRefresh;

  /// 上拉加载回调
  VoidCallback? onLoadMore;

  /// 子类，必须是ListView
  Widget child;

  @override
  State<StatefulWidget> createState() => _PullSmartRefreshWidgetState();
}

class _PullSmartRefreshWidgetState extends State<PullSmartRefreshWidget>
    with AutomaticKeepAliveClientMixin {
  /// 内部维护[RefreshController]控制器，不暴露出去
  RefreshController controller = RefreshController(initialRefresh: false);

  @override
  void initState() {
    super.initState();
    widget.getController.initPullLoading(controller);
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
        alignment: Alignment.center, // 居中
        children: [
          ScrollConfiguration(
              behavior: OverScrollBehavior(),
              child: SmartRefresher(
                controller: controller,
                enablePullDown: widget.enablePullDown,
                enablePullUp: widget.enablePullUp,
                onRefresh: () {
                  widget.getController.onLoadRefresh(controller);
                  if (widget.onRefresh != null) {
                    widget.onRefresh!();
                  }
                },
                onLoading: () {
                  widget.getController.onLoadMore(controller);
                  if (widget.onLoadMore != null) {
                    widget.onLoadMore!();
                  }
                },
                header: CustomHeader(//自定义头部
                    builder: (BuildContext context, RefreshStatus? mode) {
                  return _buildHeader(mode);
                }),
                footer: CustomFooter(// 自定义底部
                    builder: (BuildContext context, LoadStatus? mode) {
                  return _buildFooter(mode);
                }),
              )),

          /// 加载中显示的动画
          Obx(() => Visibility(
              visible: widget.getController.loadState.value,
              child: Lottie.asset(R.assetsLottiePageLoading,
                  width: 200, animate: true)))
        ]);
  }

  @override
  bool get wantKeepAlive => true;

  /// 自定义头部
  /// * [mode] 下拉刷新状态
  Widget _buildHeader(RefreshStatus? mode) {
    Widget header;
    if (mode == RefreshStatus.idle) {
      // 下拉时显示
      header =
          Text(Strings.refreshHeaderIdle.tr, style: TextStyles.style_B8C0D4_14);
    } else if (mode == RefreshStatus.refreshing) {
      // 加载中
      header =
          Lottie.asset(R.assetsLottieRefreshHeader, width: 100, animate: true);
    } else if (mode == RefreshStatus.failed) {
      // 加载失败
      header = Text(Strings.refreshHeaderFailed.tr,
          style: TextStyles.style_B8C0D4_14);
    } else if (mode == RefreshStatus.completed) {
      // 加载完成
      header = Text(Strings.refreshHeaderSuccess.tr,
          style: TextStyles.style_B8C0D4_14);
    } else {
      // 释放
      header = Text(Strings.refreshHeaderFreed.tr,
          style: TextStyles.style_B8C0D4_14);
    }
    return SizedBox(
      height: 64,
      child: Center(child: header),
    );
  }

  /// 自定义底部
  /// * [mode] 上拉加载状态
  Widget _buildFooter(LoadStatus? mode) {
    Widget footer;
    if (mode == LoadStatus.idle) {
      // 上拉时显示
      footer =
          Text(Strings.refreshPullUp.tr, style: TextStyles.style_B8C0D4_14);
    } else if (mode == LoadStatus.loading) {
      // 加载中
      footer =
          Lottie.asset(R.assetsLottieRefreshFooter, width: 200, animate: true);
    } else if (mode == LoadStatus.failed) {
      // 加载失败
      footer = Text(Strings.refreshError.tr, style: TextStyles.style_B8C0D4_14);
    } else {
      // 没有更多
      footer =
          Text(Strings.refreshNoData.tr, style: TextStyles.style_B8C0D4_14);
    }
    return SizedBox(height: 64, child: Center(child: footer));
  }
}
